<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of report
 *
 * @author Константин Охотник
 */

require_once 'orders.php';

class Client extends Orders
{
    public function __construct()
    {
        parent::__construct();
        $this->issetSession('Client');
        $this->load->model('ClientModel');
        $site = $this->ClientModel->GetSite();
        $region = $this->ClientModel->Region();
        $this->crud->assign('site',$site)->assign('region', $region);
    }
       
    
    public function index()
    {
        $content_array = $this->ClientModel->getClient(base_url()."client/index/", PER_PAGE_CRM);
        $pages = $this->pagination->create_links();
        $this->crud->assign('client' ,$content_array)
        ->assign('pages', $pages)->display('client.tpl');
    }
    
    public function city()
    {
        die(json_encode($this->ClientModel->City($this->input->post())));
    }
    
    public function add()
    {
       die( $this->ClientModel->Add($this->input->post()));
    }
    
    public function issetphone()
    {
        $p = $this->input->post();
        echo(json_encode(array(0 =>$this->ClientModel->row_nums('tb_client', 'phone', $p['phone']))));
    }
    
    public function delete($id)
    {
        $this->ClientModel->delete_post('tb_client', 'id', $id);
        $this->ClientModel->delete_post('tb_order','id_client',$id);
        redirect(site_url("client/"), 'refresh');
    }
    
    public function find()
    {
        $arr =$this->input->get();
        if( $arr['name'] !='' || $arr['phone']!='' ||  $arr['id_site'] !='')
        {
            $content_array = $this->ClientModel->Filter(base_url()."client/find/",$this->input->get(), PER_PAGE_CRM);
            $pages = $this->pagination->create_links();

            if( isset($content_array['error']))
            {
                $this->crud->assign('error' ,$content_array)->display('client.tpl'); 
            }
            else
            {
                $this->crud->assign('client' ,$content_array)
                    ->assign('pages', $pages)->display('client.tpl');
            }
        }
        else
        {
            redirect(site_url("client/"), 'refresh');
        }
    }
}